package com.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dto.PurchaseDto;
import com.po.Purchase;
import com.result.Result;
import com.service.PurchaseService;
import com.vo.PurchaseVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/purchase/")
public class PurchaseController {

    private final PurchaseService purchaseService;

    /**
     * 根据单品id发起拼单
     * @param ids
     * @return
     */
    @PostMapping("save")
    public Result save(@RequestParam String ids){
        log.info("根据商品id发起拼单:{}",ids);
        purchaseService.isave(ids);
        return Result.success();
    }

    /**
     * 根据商品id批量查询该商品包含的拼单信息
     * @param id
     * @return
     */
    @GetMapping("list")
    public Result<List<PurchaseVo>> list(@RequestParam Long id){
        log.info("根据商品id批量查询该商品包含的拼单信息:{}",id);
        List<PurchaseVo> purchaseVoList = purchaseService.ilist(id);
        return Result.success(purchaseVoList);
    }

    /**
     * 加入拼单
     * @param purchaseDto
     * @return
     */
    @PostMapping("add")
    public Result add(@RequestBody PurchaseDto purchaseDto){
        log.info("加入拼单：{}",purchaseDto);
        purchaseService.add(purchaseDto);
        return Result.success();
    }

    /**
     * 根据拼单id查询详情
     * @param id
     * @return
     */
    @GetMapping("get")
    public Result<PurchaseVo> get(@RequestParam Long id){
        log.info("根据拼单id查询详情:{}",id);
        PurchaseVo purchaseVo = purchaseService.get(id);
        return Result.success(purchaseVo);
    }

    /**
     * 查询用户所有拼单数据
     * @return
     */
    @GetMapping("mylist")
    public Result<List<PurchaseVo>> mylist(){
        log.info("查询用户所有拼单数据:");
        List<PurchaseVo> purchaseVoList = purchaseService.mylist();
        return Result.success(purchaseVoList);
    }

    /**
     * 拼单超时自动取消
     * @param id
     * @return
     */
    @PutMapping("delete")
    public Result delete(@RequestParam Long id){
        log.info("拼单超时自动取消:{}",id);
        purchaseService.delete(id);
        return Result.success();
    }
}
